package com.lei.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lei.statistics.client.UcenterClient;
import com.lei.statistics.entity.StatisticsDaily;
import com.lei.statistics.mapper.StatisticsDailyMapper;
import com.lei.statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-18
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        baseMapper.delete(new QueryWrapper<StatisticsDaily>().eq("date_calculated",day));
        Integer countRegister = ucenterClient.countRegister(day);



        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);
        sta.setDateCalculated(day);
        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));

        baseMapper.insert(sta);
    }

    @Override
    public Map<String, Object> getData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        ArrayList<String> dateList = new ArrayList<String>();
        ArrayList<Integer> dataList = new ArrayList<>();
        if ("login_num".equals(type)){
            for (StatisticsDaily statisticsDaily : staList) {
                dateList.add(statisticsDaily.getDateCalculated());
                dataList.add(statisticsDaily.getLoginNum());
            }
        } else if ("register_num".equals(type)){
            for (StatisticsDaily statisticsDaily : staList) {
                dateList.add(statisticsDaily.getDateCalculated());
                dataList.add(statisticsDaily.getRegisterNum());
            }
        } else if ("video_view_num".equals(type)){
            for (StatisticsDaily statisticsDaily : staList) {
                dateList.add(statisticsDaily.getDateCalculated());
                dataList.add(statisticsDaily.getVideoViewNum());
            }
        }else{
            for (StatisticsDaily statisticsDaily : staList) {
                dateList.add(statisticsDaily.getDateCalculated());
                dataList.add(statisticsDaily.getCourseNum());
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("dataList",dataList);
        return map;
    }
}
